package com.wzl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzl.dao.TreatmentInfoMapper;
import com.wzl.entity.dto.TreatmentInfoDTO;
import com.wzl.service.TreatmentInfoService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class TreatmentInfoServiceImpl extends ServiceImpl<TreatmentInfoMapper, TreatmentInfoDTO> implements TreatmentInfoService {
    @Override
    public Page<TreatmentInfoDTO> getPage(Page<TreatmentInfoDTO> page, TreatmentInfoDTO treatmentInfoDTO) {
        QueryWrapper<TreatmentInfoDTO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(treatmentInfoDTO.getTreatId())) {
            queryWrapper.eq("treat_id", treatmentInfoDTO.getTreatId());
        }
        if (StringUtils.isNotEmpty(treatmentInfoDTO.getTime())) {
            queryWrapper.eq("time", treatmentInfoDTO.getTime());
        }
        Page<TreatmentInfoDTO> treatmentInfoDTOPage = this.baseMapper.selectPage(page, queryWrapper);
        return treatmentInfoDTOPage;
    }

    @Override
    public boolean saveTreat(TreatmentInfoDTO treatmentInfoDTO) {
        String treatId = RandomStringUtils.randomAlphanumeric(4);
        treatmentInfoDTO.setId(treatId + treatmentInfoDTO.getTreatId());
        return this.baseMapper.insert(treatmentInfoDTO) > 0;
    }

    @Override
    public boolean updateTreatmentInfo(TreatmentInfoDTO treatmentInfoDTO) {
        return this.baseMapper.updateById(treatmentInfoDTO) > 0;
    }

    @Override
    public TreatmentInfoDTO treatmentInfoDetail(String id) {
        return this.baseMapper.selectById(id);
    }
}
